<?php

class Admin_ArticleController extends Zend_Controller_Action
{

    public function init()
    {
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/article.css');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
    }

    public function indexAction()
    {
        sleep(200);
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
                $db = Zend_Db::factory($config->resources->db);
                $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('txy_article',array('article_id','article_title')));
        		//$adapter=new Txy_Extends_TempTaobaoPageinator($db->select()->from('txy_temp_taobao')->where('item_status=2'));
                $paginator = new Zend_Paginator($adapter);
                $paginator->setItemCountPerPage(50);
                $paginator->setCurrentPageNumber($this->_getParam('page'));
                $this->view->paginator = $paginator;
    }

    public function newAction()
    {
        $this->view->headScript()->appendFile($this->view->baseUrl().'/tiny_mce/tiny_mce.js');
                                	
                            // action body
                            //$articleModel=new Application_Model_DbTable_Article();
                            $form=new Application_Form_NewArticle();
                            $this->view->form=$form;
                                
                                if($this->getRequest()->isPost()){
                                	$formData=$this->getRequest()->getPost();
                                	if($form->isValid($formData)){
                        				 $articleModel=new Application_Model_DbTable_Article();
                        				 $data=array(
                        		    		'article_title'=>$form->getValue('article_title'),
                        		    		'article_url_spell'=>$form->getValue('article_url_spell'),
                        		    		'article_key_words'=>$form->getValue('article_key_words'),
                        		    		'article_author_id'=>'1',
                        				 	'article_create_date'=>new Zend_Db_Expr('now()'),
                        		    		'article_description'=>$form->getValue('article_description'),
                        		    		'article_content'=>$form->getValue('article_content'),
                        		    		'article_type'=>$form->getValue('article_type'),
                        				 	'article_content_type'=>$form->getValue('article_content_type'),
                        		    		'article_from'=>$form->getValue('article_from')
                        		    	);
                        				 $articleModel->addNewArticle($data);       		
                                	}
    							}
    }

    public function updateAction()
    {
        // action body
                $req=$this->getRequest();
                $aid=$req->getParam('aid');
                $db_article=new Application_Model_DbTable_Article();
                $article=$db_article->getArticleById($aid);
                $form=new Application_Form_UpdateArticle();
                $form->getElement('article_id')->setValue($article->article_id);
                $form->getElement('article_title')->setValue($article->article_title);
                $form->getElement('article_url_spell')->setValue($article->article_url_spell);
                $form->getElement('article_key_words')->setValue($article->article_key_words);
                $form->getElement('article_description')->setValue($article->article_description);
                $form->getElement('article_content')->setValue($article->article_content);
                $form->getElement('article_type')->setValue($article->article_type);
                $form->getElement('article_content_type')->setValue($article->article_content_type);
                $form->getElement('article_from')->setValue($article->article_from);
                $this->view->form=$form;
    }

    public function ajaxGetPinYinUrlAction()
    {
        $this->_helper->layout()->disableLayout();
                        $req=$this->getRequest();
                        $article_title=$req->getParam('article_title');
                        $pinYin=new Txy_Util_PinYin();
                        echo $pinYin->getPinYin(mb_convert_encoding($article_title,'gbk','utf-8'));
    }

    public function doUpdateAction()
    {
	//actionbody
		$form=new Application_Form_UpdateArticle();
		if($this->getRequest()->isPost()){
			$formData=$this->getRequest()->getPost();
			if($form->isValid($formData)){
				$articleModel=new Application_Model_DbTable_Article();
				$data=array(
				'article_title'=>$form->getValue('article_title'),
				'article_url_spell'=>$form->getValue('article_url_spell'),
				'article_key_words'=>$form->getValue('article_key_words'),
				'article_description'=>$form->getValue('article_description'),
				'article_content'=>$form->getValue('article_content'),
				'article_type'=>$form->getValue('article_type'),
				'article_content_type'=>$form->getValue('article_content_type'),
				'article_from'=>$form->getValue('article_from')
				);
				$db_article=new Application_Model_DbTable_Article();
				$db_article->updateArticleById($data, $form->getValue('article_id'));
				//$articleModel->addNewArticle($data);		
			}
		}
    }


}









